use "${clean}analysis.dta", clear
keep if north == 1 
drop if congress > 55
drop if state == "District of Columbia"

//standardizing variables
egen birth_state_surban = std(birth_state_urban_percent)
egen birth_state_srugged = std(birth_state_ruggedness)
egen birth_state_spostoffice = std(birth_state_postoffice_area)
egen sborn = std(born)

// creating a variable to keep the sample constant
cap drop sample
gen sample = 1
foreach var of varlist $state_bcontrols $life_controls {
	replace sample = . if `var' == .
}
keep if sample == 1

// dropping third parties
drop if partyid == 2 | partyid == .

// dropping WHIGS
replace partyid = . if party_name == "Whig"
replace partyid = 1 if bioguide == "B000376"
replace partyid = 1 if bioguide == "S000261"
replace partyid = 1 if bioguide == "S000801"


// creating some variables
local c = 1860

gen period = year > `c'
replace year = year - `c'


* regressions
* ------------

estimates clear

// republican baseline mixed model
eststo: mixed singular c.year##i.period##ib1.partyid ${state_bcontrols} ${life_controls} if sample == 1 || _all:R.congress || bioguide_id:

// long version for appendix
esttab est* using "${output}TabD15_congress_north_demrep_statecontrols_fulltable.tex", replace ///
	b(%8.3f) se(%8.3f) transform(ln*: exp(@) exp(@)) ///
	starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
	keep( ///
		year ///
		1.period  ///
		0.partyid ///
		1.period#c.year ///
		0.partyid#c.year ///
		1.period#0.partyid ///
		1.period#0.partyid#c.year ///
		birth_state_surban birth_state_spostoffice birth_state_srugged birth_state_anyriver birth_state_anycanal ///
		sborn college usmilitary ///
		_cons) ///
	order( ///
		year ///
		1.period  ///
		0.partyid ///
		1.period#c.year ///
		0.partyid#c.year ///
		1.period#0.partyid ///
		1.period#0.partyid#c.year ///
		birth_state_surban birth_state_spostoffice birth_state_srugged birth_state_anyriver birth_state_anycanal ///
		sborn college usmilitary ///
		_cons) ///
	coeflabels( ///
		year "Year of Speech" ///
		1.period "After 1860" ///
		0.partyid "Democrat" ///
		1.period#c.year "After 1860 $\times$ Year of Speech" ///
		0.partyid#c.year "Democrat $\times$ Year of Speech" ///
		1.period#0.partyid "After 1860 $\times$ Democrat" ///
		1.period#0.partyid#c.year "After 1860 $\times$ Democrat $\times$ Year of Speech" ///
		birth_state_surban "State of Birth: \% Urban (std)" /// 
		birth_state_spostoffice "State of Birth: Post Office Density (std)" ///
		birth_state_srugged "State of Birth: Terrain Ruggedness (std)" ///
		birth_state_anyriver "Born on River" ///
		birth_state_anycanal "Born on Canal" ///
		sborn "Year of Birth (std)" ///
		college "Attended College" ///
		usmilitary "Served in US Military" ///
		_cons "Constant" ///
		) ///
	nodepvars nomtitles nonotes ///
	eqlabels("" "sd(Congress)" "sd(Bioguide)" "sd(Residual)", none) ///
	nogaps nolines posthead(\midrule) prefoot(\midrule) postfoot("\bottomrule \end{tabular}}") ///
	tex compress	
